//
//  OneButtonCellData.m
//  18Birdies
//
//  Created by poleness on 2016/10/31.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "OneButtonCellData.h"
#import "OneButtonCell.h"

@implementation OneButtonCellData

+ (instancetype)cellDataWithTitle:(NSString*)title
                      buttonStyle:(ButtonStyle)buttonStyle
                     buttonHeight:(int)buttonHeight
                    onClickHandle:(OnCellClickedHanlder)onCellClickedHanlder {
  OneButtonCellData* cellData = [[self alloc] init];
  cellData->_title = title.copy;
  cellData->_buttonHeight = buttonHeight;
  cellData->_buttonStyle = buttonStyle;
  cellData->_buttonClickedHandler = onCellClickedHanlder;
  cellData.buttonEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
  cellData.buttonLeftMargin = 10;
  cellData.clickable = NO;
  return cellData;
}

+ (instancetype)cellDataWithTitle:(NSString*)title
              backgroundImageName:(NSString*)backgroundImageName
                     buttonHeight:(int)buttonHeight
                    onClickHandle:(OnCellClickedHanlder)onCellClickedHanlder {
  OneButtonCellData* cellData = [[self alloc] init];
  cellData->_title = title.copy;
  cellData->_buttonHeight = buttonHeight;
  cellData->_backgroundImageName = backgroundImageName;
  cellData->_buttonClickedHandler = onCellClickedHanlder;
  cellData.buttonEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
  cellData.buttonLeftMargin = 10;
  cellData.clickable = NO;
  return cellData;
}

- (Class)cellClazz {
  return OneButtonCell.class;
}

- (NSString*)nibName {
  return @"OneButtonCell";
}

- (void)refreshCell:(OneButtonCell*)cell {
  [super refreshCell:cell];
  self.cellHeight = self.buttonHeight + self.buttonEdgeInsets.top + self.buttonEdgeInsets.bottom;
  cell.hintImageView.hidden = !self.showHint;
  cell.buttonHeight.constant = self.buttonHeight;
  cell.button.contentHorizontalAlignment = self.contentHorizontalAlignment;
  cell.button.contentVerticalAlignment = self.contentVerticalAlignment;
  if (self.attributedTitle.length > 0) {
    [cell.button setTitle:nil forState:UIControlStateNormal];
    [cell.button setTitle:nil forState:UIControlStateHighlighted];
    [cell.button setAttributedTitle:self.attributedTitle forState:UIControlStateNormal];
    [cell.button setAttributedTitle:self.attributedTitle forState:UIControlStateHighlighted];
  } else {
    [cell.button setTitle:self.title forState:UIControlStateNormal];
    [cell.button setTitle:self.title forState:UIControlStateHighlighted];
    [cell.button setAttributedTitle:nil forState:UIControlStateNormal];
    [cell.button setAttributedTitle:nil forState:UIControlStateHighlighted];
  }

  cell.backgroundColor = self.backGroundColor != nil ? self.backGroundColor : [UIColor whiteColor];
  cell.button.userInteractionEnabled = YES;
  if (self.backgroundImageName.length > 0) {
    [cell.button setBackgroundImage:[UIImage imageNamed:self.backgroundImageName] forAllState:YES];
  } else {
    if (self.buttonStyle == BUTTON_BLUE_BORDER_STYLE) {
      [cell.button setBlueBorderedStyle];
      cell.button.backgroundColor = [UIColor whiteColor];
    } else if (self.buttonStyle == BUTTON_BLUE_BACKGROUND_STYLE) {
      [cell.button setBlueStyle];
    } else if (self.buttonStyle == BUTTON_BLUE_LINEAR_GRADIENT_STYLE) {
      [cell.button setBlueLinerGradientStyle];
    } else if (self.buttonStyle == BUTTON_WHITE_BACKGROUND_STYLE) {
      [cell.button setWhiteBackgroundStyle:LC_COLOR_WHITE];
    } else if (self.buttonStyle == BUTTON_BLUE_FONT_NO_BG_STYLE) {
      [cell.button setBlueNoBgImageStyle];
    } else if (self.buttonStyle == BUTTON_GRAY_BACKGROUND_STYLE ||
               self.buttonStyle == BUTTON_GRAY_FONT_DISABLED_STYLE) {
      [cell.button setSecondaryGrayStyle];
      cell.button.userInteractionEnabled = NO;

      if (self.buttonStyle == BUTTON_GRAY_FONT_DISABLED_STYLE) {
        self.imageName = nil;
        self.imageNameForHighlighted = nil;
      }
    } else if (self.buttonStyle == BUTTON_RED_FONT_BORDER_STYLE) {
      [cell.button setRedFontBorderedStyle];
    } else if (self.buttonStyle == BUTTON_BLUE_FONT_GRAY_HIGHLITED_STYLE) {
      [cell.button setBlueCellStyle];
    } else if (self.buttonStyle == BUTTON_BLUE_FONT_WHITE_BG_STYLE) {
      [cell.button setWhiteStyle];
    } else if (self.buttonStyle == BUTTON_DRAK_FONT_WHITE_BG_STYLE) {
      [cell.button setDarkCellStyle];
    } else if (self.buttonStyle == BUTTON_RED_FONT_WHITE_BG_STYLE) {
      [cell.button setRedFontStyle];
    } else if (self.buttonStyle == BUTTON_WHITE_FONT_ORANGE_BG_STYLE) {
      [cell.button setOrangeStyle];
    }

    if (self.buttonBackGroundColor != nil) {
      cell.button.backgroundColor = self.buttonBackGroundColor;
    }
  }

  if (self.buttonLcFont != nil) {
    [cell.button setLcFont:self.buttonLcFont];
  }
  if (self.buttonHighlitedFont != nil) {
    [cell.button setTitleColor:self.buttonHighlitedFont.color forState:UIControlStateHighlighted];
  }
  if (self.buttonHighlitedBackGroundColor != nil) {
    [cell.button setBackgroundImage:[UIImage imageWithColor:self.buttonHighlitedBackGroundColor]
                           forState:UIControlStateHighlighted];
  }

  if (self.imageName.length > 0) {
    if (self.buttonRightMargin > 0) {
      [cell.button addImage:[UIImage imageNamed:self.imageName] rightMargin:self.buttonRightMargin];
    } else {
      [cell.button addImage:[UIImage imageNamed:self.imageName] leftMargin:self.buttonLeftMargin];
    }
    if (self.imageNameForHighlighted.length > 0) {
      [cell.button setImage:[UIImage imageNamed:self.imageNameForHighlighted]
                   forState:UIControlStateHighlighted];
    }
    cell.button.adjustsImageWhenHighlighted = NO;
  } else {
    [cell.button setImage:nil forState:UIControlStateNormal];
    [cell.button setImage:nil forState:UIControlStateHighlighted];
    cell.button.imageEdgeInsets = UIEdgeInsetsZero;
    cell.button.titleEdgeInsets = UIEdgeInsetsZero;
  }

  cell.buttonTopMargin.constant = self.buttonEdgeInsets.top;
  cell.buttonLeftMargin.constant = self.buttonEdgeInsets.left;
  cell.buttonRightMargin.constant = self.buttonEdgeInsets.right;
  cell.buttonBottomMargin.constant = self.buttonEdgeInsets.bottom;
  if (self.cornerRadius > 0) {
    cell.button.layer.cornerRadius = self.cornerRadius;
    cell.button.layer.masksToBounds = YES;
  }
  cell.shadowImageView.hidden = !self.showBottomShadow;

  [cell.button removeAllTouchUpInsideAction];
  [cell.button addTarget:self
                  action:@selector(buttonClicked:)
        forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked:(UIButton*)button {
  if (self.buttonClickedHandler != nil) {
    self.buttonClickedHandler(self);
  }
}

@end
